package com.example.asus_pc.myshop.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.example.asus_pc.myshop.utils.UIUtils;

/**
 * Created by ：WuGaoJie on 2018/6/7
 */
public class LetterListView extends View {

	private OnTouchingLetterChangedListener onTouchingLetterChangedListener;
	private String[] b = {};
	int choose = -1;
	private int height;
	private int width;
	private int txtSize;

	private int num_all=26;

	private Paint paint = new Paint();

	public LetterListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public LetterListView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public LetterListView(Context context) {
		super(context);
	}

	public void updateData(String[] data) {
		b = data;
		invalidate();
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		this.height = getDefaultSize(getSuggestedMinimumHeight(),
				heightMeasureSpec);
		this.width = getDefaultSize(getSuggestedMinimumWidth(),
				widthMeasureSpec);
		setMeasuredDimension(width, height);
		txtSize = 13;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		int singleHeight = height / (num_all);
		for (int i = 0; i < b.length; i++) {
			paint.setColor(Color.parseColor("#23abdf"));
			paint.setTypeface(Typeface.DEFAULT_BOLD);

			paint.setTextSize(UIUtils.dp2px(txtSize));
			paint.setAntiAlias(true);
			if (i == choose) {
				
			}
			float xPos = width / 2 - paint.measureText(b[i]) / 2;
			float yPos = singleHeight * i + singleHeight;
			canvas.drawText(b[i], xPos, yPos, paint);
			paint.reset();
		}

	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		final int action = event.getAction();
		final float y = event.getY();
		final int oldChoose = choose;
		final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
		final int c = (int) (y / getHeight() * (num_all));
		
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			if (oldChoose != c && listener != null) {
				if (c >= 0 && c < num_all) {
					if(c<b.length){
						listener.onTouchingLetterChanged(1, b[c]);
						choose = c;
						invalidate();
					}
				}
			}

			break;
		case MotionEvent.ACTION_MOVE:
			if (oldChoose != c && listener != null) {
				if (c >= 0 && c < num_all) {
					if(c<b.length){
						listener.onTouchingLetterChanged(1, b[c]);
						choose = c;
						invalidate();
					}
				}
			}
			break;
		case MotionEvent.ACTION_UP:
			choose = -1;
			invalidate();
			if (listener != null)
				listener.onTouchingLetterChanged(0, null);
			break;
		}
		return true;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return super.onTouchEvent(event);
	}

	public void setOnTouchingLetterChangedListener(
			OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
		this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
	}

	public interface OnTouchingLetterChangedListener {
		public void onTouchingLetterChanged(int state, String s);
	}

}
